/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hhx.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hhx.entity.HhxFocus;
import com.thinkgem.jeesite.modules.hhx.entity.HhxUser;
import com.thinkgem.jeesite.modules.hhx.service.HhxFocusService;
import com.thinkgem.jeesite.modules.hhx.service.HhxUserService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/hhx/hhxFocus")
public class HhxFocusController extends BaseController {

	@Autowired
	private HhxFocusService hhxFocusService;
	@Autowired
	private HhxUserService hhxUserService;
	
	@ModelAttribute
	public HhxFocus get(@RequestParam(required=false) String id) {
		HhxFocus entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxFocusService.get(id);
		}
		if (entity == null){
			entity = new HhxFocus();
		}
		return entity;
	}
	
	@RequiresPermissions("hhx:hhxFocus:view")
	@RequestMapping(value = {"list", ""})
	public String list(HhxFocus hhxFocus, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxFocus> page = hhxFocusService.findPage(new Page<HhxFocus>(request, response), hhxFocus); 
		model.addAttribute("page", page);
		return "modules/hhx/hhxFocusList";
	}
	

	
	@RequestMapping(value = {"wxlist", ""})
	public String wxlist(HttpServletRequest request, HttpServletResponse response,String userid) {
		List<Object[]> focuslist = hhxFocusService.getList(userid);
		return renderString(response, focuslist);
	}
	
	@RequestMapping(value = {"wxlist2", ""})
	public String wxlist2(HttpServletRequest request, HttpServletResponse response,String focusid) {
		List<Object[]> userlist = hhxFocusService.getList2(focusid);
		return renderString(response, userlist);
	}

	@RequiresPermissions("hhx:hhxFocus:view")
	@RequestMapping(value = "form")
	public String form(HhxFocus hhxFocus, Model model) {
		model.addAttribute("hhxFocus", hhxFocus);
		return "modules/hhx/hhxFocusForm";
	}

	@RequiresPermissions("hhx:hhxFocus:edit")
	@RequestMapping(value = "save")
	public String save(HhxFocus hhxFocus, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hhxFocus)){
			return form(hhxFocus, model);
		}
		hhxFocusService.save(hhxFocus);
		addMessage(redirectAttributes, "保存关注成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxFocus/?repage";
	}
	
	@RequiresPermissions("hhx:hhxFocus:edit")
	@RequestMapping(value = "delete")
	public String delete(HhxFocus hhxFocus, RedirectAttributes redirectAttributes) {
		hhxFocusService.delete(hhxFocus);
		addMessage(redirectAttributes, "删除关注成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxFocus/?repage";
	}
	
	@RequestMapping(value = "wxfocus")
	public String wxfocus(HttpServletRequest request, HttpServletResponse response,String userid,String focusid) {
		String id = UUID.randomUUID().toString().replace("-", "");
		int result = hhxFocusService.insertFocus(id,userid,focusid);
		int focuscount = hhxFocusService.countFocus(focusid);
		Map<String, String> map = new HashMap<String, String>();
		if(result>0) {
			hhxUserService.updateFocus(focusid,String.valueOf(focuscount));
			map.put("result","true");
			map.put("msg","关注成功");
		}else {
			map.put("result","false");
			map.put("msg","关注失败");
		}
		return renderString(response, map);
		
	}
	
	@RequestMapping(value = "wxquitfocus")
	public String wxquitfocus(HttpServletRequest request, HttpServletResponse response,String userid,String focusid) {
//		String id = UUID.randomUUID().toString().replace("-", "");
		int result = hhxFocusService.delFocus(userid,focusid);
		int focuscount = hhxFocusService.countFocus(focusid);
		Map<String, String> map = new HashMap<String, String>();
		if(result>0) {
			hhxUserService.updateFocus(focusid,String.valueOf(focuscount));
			map.put("result","true");
			map.put("msg","取消关注成功");
		}else {
			map.put("result","false");
			map.put("msg","取消关注失败");
		}
		return renderString(response, map);
		
	}
	
	@RequestMapping(value = "wxgetFoucsFlag")
	public String wxgetFoucsFlag(HttpServletRequest request, HttpServletResponse response,String userid,String focusid) {
		boolean focusflag = false;
		int focusnum = 0;
		focusnum = hhxFocusService.getCount(userid,focusid);
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		if(focusnum>0) {
			focusflag = true;
		}else {
			focusflag = false;
		}
		map.put("result",focusflag);
		return renderString(response, map);
		
	}

}